using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.Common.Outlook;
using TXLooker.WorkItemExtention.Core;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI
{
 
    public partial class WorkItemTypePatternCategoryColorForm : Form
    {
        private Microsoft.Office.Interop.Outlook.Application m_Application;
        private List<string> m_categories = new List<string>();
        private string m_strProjectName;
        private string m_strServerName;
        private string m_strWorkItemTypeName;
        private const int m_stateId = 0x0002;

        private string registryPath = Resources.REGISTRY_COLORCATEGORIES_PATH;

        public WorkItemTypePatternCategoryColorForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.CenterToScreen();
            this.m_state.Enabled = false;
            this.m_color.Enabled = false;
            this.m_Application = ExtentionEntryPoint.Application;
            this.m_workItemTypeMaps.SelectedNodeChanged += new EventHandler(m_workItemTypeMaps_SelectedNodeChanged);
            m_color.ImageList = m_imageListColorCategories;
            InitImageList();
            InitColorCategories();
        }

        private void InitColorCategories()
        {
            Dictionary<string, string> dicCategories = OutlookGlobal.GetOlCategoriesColor(this.m_Application);
            for (int i = 0; i < m_imageListColorCategories.Images.Count; i++)
            {
                foreach (KeyValuePair<string, string> kvp in dicCategories)
                {
                    if (kvp.Key == m_imageListColorCategories.Images.Keys[i])
                    {
                        this.m_color.Items.Add(new ImageComboItem(kvp.Value, i));
                    }
                }
            }
            Dictionary<string, string>.ValueCollection values = dicCategories.Values;
            foreach (string s in values)
            {
                m_color.Text = s;
                break;
            }
        }

        private void InitImageList()
        {
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorBlack.ToString(), Resources.Black_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorBlue.ToString(), Resources.Blue_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkBlue.ToString(), Resources.DarkBlue_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkGray.ToString(), Resources.DarkGray_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkGreen.ToString(), Resources.DarkGreen_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkMaroon.ToString(), Resources.DarkMaroon_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkOlive.ToString(), Resources.DarkOlive_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkOrange.ToString(), Resources.DarkOrange_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkPeach.ToString(), Resources.DarkPeach_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkPurple.ToString(), Resources.DarkPurple_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkRed.ToString(), Resources.DarkRed_Squere);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkSteel.ToString(), Resources.DarkSteel_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkTeal.ToString(), Resources.DarkTeal_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorDarkYellow.ToString(), Resources.DarkYellow_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorGray.ToString(), Resources.Gray_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorGreen.ToString(), Resources.Green_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorMaroon.ToString(), Resources.Maroon_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorOlive.ToString(), Resources.Olive_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorOrange.ToString(), Resources.Orange_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorPeach.ToString(), Resources.Peach_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorPurple.ToString(), Resources.Purple_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorRed.ToString(), Resources.Red_Squere);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorSteel.ToString(), Resources.Steel_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorTeal.ToString(), Resources.Teal_Square);
            m_imageListColorCategories.Images.Add(OlCategoryColor.olCategoryColorYellow.ToString(), Resources.Yellow_Square);
        }

        void m_workItemTypeMaps_SelectedNodeChanged(object sender, EventArgs e)
        {
            WorkItemType workItemType = m_workItemTypeMaps.WorkItemType;
            bool bEnable = (workItemType != null);
            if (bEnable)
                SaveColorCategories();
            this.m_state.Enabled = bEnable;
            this.m_buttonSave.Enabled = bEnable;
            this.m_color.Enabled = bEnable;

            FieldDefinitionCollection fields = m_workItemTypeMaps.WorkItemType.FieldDefinitions;
            List<string> stateValues = new List<string>();

            this.m_strProjectName = m_workItemTypeMaps.ProjectProvider.FolderName;
            this.m_strServerName = m_workItemTypeMaps.ServerProvider.FolderName;
            this.m_strWorkItemTypeName = m_workItemTypeMaps.WorkItemType.Name;

            this.m_state.Items.Clear();
            foreach (FieldDefinition field in fields)
            {
                if (field.Id == m_stateId)
                {
                    AllowedValuesCollection values = field.AllowedValues;
                    foreach (string str in values)
                    {
                        stateValues.Add(str);
                    }
                }
            }

            foreach (string str in stateValues)
            {
                this.m_state.Items.Add(str);
            }
            this.m_state.Text = stateValues[0];
        }

        
        private void OnSave(object sender, EventArgs e)
        {
            SaveColorCategories();
        }

        private void SaveColorCategories()
        {
            ColorPattern colorPattern = new ColorPattern(this.m_strProjectName, this.m_strWorkItemTypeName, this.m_state.Text);
            colorPattern.Save(m_color.Text);
            GC.Collect();
        }

        private void OnSelectedState(object sender, EventArgs e)
        {
            LoadColorCategories();
        }

        private void LoadColorCategories()
        {
            ColorPattern colorPattern = new ColorPattern(this.m_strProjectName,this.m_strWorkItemTypeName,this.m_state.Text);
            this.m_color.Text = colorPattern.Load(m_Application);
            GC.Collect();            
        }
    }
}